package com.knshare.utils.Executor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 默认工厂
 * @author rongxun.wu01
 * @product IntelliJ IDEA
 * @project KnShare
 * @Date 2023-05-06 14:05
 */
public class ThreadFactoryDefault implements ThreadFactory {

    /**
     * 线程名前缀
     */
    private final String prefix;

    /**
     * 线程编号
     */
    private final AtomicInteger threadNumber = new AtomicInteger(1);

    /**
     * 创建线程工厂
     *
     * @param prefix 线程名前缀 帮助查看日志
     */
    public ThreadFactoryDefault(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(null, r, prefix + threadNumber.getAndIncrement());
    }
}
